package com.ruicar.afs.jobadmin.core.route;

import com.ruicar.afs.jobadmin.core.route.strategy.*;
import com.ruicar.afs.jobadmin.core.util.I18nUtil;

/**
 * The type ExecutorRouteStrategyEnum
 * <p>Description: </p>
 *
 * @author Fzero
 * @version 1.0
 * @date Created on 2020.06.13 14:17:10
 * @since 1.0
 */
public enum ExecutorRouteStrategyEnum {

    /**
     * First
     * <p>Description: </p>
     */
    FIRST(I18nUtil.getString("jobconf_route_first"), new ExecutorRouteFirst()),
    /**
     * Last
     * <p>Description: </p>
     */
    LAST(I18nUtil.getString("jobconf_route_last"), new ExecutorRouteLast()),
    /**
     * Round
     * <p>Description: </p>
     */
    ROUND(I18nUtil.getString("jobconf_route_round"), new ExecutorRouteRound()),
    /**
     * Random
     * <p>Description: </p>
     */
    RANDOM(I18nUtil.getString("jobconf_route_random"), new ExecutorRouteRandom()),
    /**
     * Consistent hash
     * <p>Description: </p>
     */
    CONSISTENT_HASH(I18nUtil.getString("jobconf_route_consistenthash"), new ExecutorRouteConsistentHash()),
    /**
     * Least frequently used
     * <p>Description: </p>
     */
    LEAST_FREQUENTLY_USED(I18nUtil.getString("jobconf_route_lfu"), new ExecutorRouteLFU()),
    /**
     * Least recently used
     * <p>Description: </p>
     */
    LEAST_RECENTLY_USED(I18nUtil.getString("jobconf_route_lru"), new ExecutorRouteLRU()),
    /**
     * Failover
     * <p>Description: </p>
     */
    FAILOVER(I18nUtil.getString("jobconf_route_failover"), new ExecutorRouteFailover()),
    /**
     * Busyover
     * <p>Description: </p>
     */
    BUSYOVER(I18nUtil.getString("jobconf_route_busyover"), new ExecutorRouteBusyover()),
    /**
     * Sharding broadcast executor route strategy enum
     * <p>Description: </p>
     */
    SHARDING_BROADCAST(I18nUtil.getString("jobconf_route_shard"), null);

    /**
     * Executor route strategy enum
     *
     * @param title  title
     * @param router router
     */
    ExecutorRouteStrategyEnum(String title, ExecutorRouter router) {
        this.title = title;
        this.router = router;
    }

    /**
     * Title
     * <p>Description: </p>
     */
    private String title;
    /**
     * Router
     * <p>Description: </p>
     */
    private ExecutorRouter router;

    /**
     * Gets title *
     *
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * Gets router *
     *
     * @return the router
     */
    public ExecutorRouter getRouter() {
        return router;
    }

    /**
     * Match executor route strategy enum
     *
     * @param name        name
     * @param defaultItem default item
     * @return the executor route strategy enum
     */
    public static ExecutorRouteStrategyEnum match(String name, ExecutorRouteStrategyEnum defaultItem) {
        if (name != null) {
            for (ExecutorRouteStrategyEnum item : ExecutorRouteStrategyEnum.values()) {
                if (item.name().equals(name)) {
                    return item;
                }
            }
        }
        return defaultItem;
    }

}
